Page({
  data: {
    beginDate: "",
    endDate: "",
    activeIndex: 2,
    dateOptionsList: [],
  },
  onLoad: function (options) {
    // 初始化
    this.initDateOptionList();
  },
  // 本季的开始月份
  getQuarterStartMonth(nowMonth) {
    let quarterStartMonth = 0;
    if (nowMonth < 3) {
      quarterStartMonth = 0;
    }
    if (2 < nowMonth && nowMonth < 6) {
      quarterStartMonth = 3;
    }
    if (5 < nowMonth && nowMonth < 9) {
      quarterStartMonth = 6;
    }
    if (nowMonth > 8) {
      quarterStartMonth = 9;
    }
    return quarterStartMonth;
  },
  // 获得某月的天数
  getMonthDays(year, month) {
    let monthStartDate = new Date(year, month, 1);
    let monthEndDate = new Date(year, month + 1, 1);
    let days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
    return days;
  },
  // 日期格式化
  formatDate(params){
    let date = new Date(params);
    let y = date.getFullYear();
    let m = date.getMonth()+1;
    let d = date.getDate();
    m = m < 10 ? '0' + m : m;
    d = d < 10 ? '0' + d : d;
    return `${y}-${m}-${d}`;
  },
  // 初始化日期选项
  initDateOptionList() {
    let curDate = new Date(); // 当前日期
    let curDayOfWeek = curDate.getDay(); // 本周的第几天
    let curDay = curDate.getDate(); // 当前日
    let curMonth = curDate.getMonth(); // 当前月
    let curYear = curDate.getFullYear(); // 当前年
    let lastDateWeekAgo = new Date(curDate.getTime() - 1000 * 3600 * 24 * 7); //上周
    // let lastDateWeekAgoOfWeek = lastDateWeekAgo.getDay(); // 上周的第几天
    let lastYear = curYear - 1; // 去年

    let yesterday = new Date(curDate.getTime() - 1000 * 3600 * 24); // 昨天
    let curDateWeekAgo = new Date(curDate.getTime() - 1000 * 3600 * 24 * 6); // 7天前
    let curDateMonthAgo = new Date(curDate.getTime() - 1000 * 3600 * 24 * 29); // 30天前
    // 本周的开始日期
    let weekStartDate = new Date(
      curDate - (curDayOfWeek - 1) * 1000 * 3600 * 24
    );
    // 本周的结束日期
    let weekEndDate = new Date((weekStartDate / 1000 + 6 * 3600 * 24) * 1000);
    // 上周的开始日期
    let lastWeekStartDate = new Date(
      curYear,
      curMonth,
      curDay - curDayOfWeek - 6
    );
    // 上周的结束日期
    let lastWeekEndDate = new Date(curYear, curMonth, curDay - curDayOfWeek);
    // 本月的开始日期
    let curMonthStartDate = new Date(curYear, curMonth, 1);
    // 本月的结束日期
    let curMonthEndDate = new Date(
      curYear,
      curMonth,
      this.getMonthDays(curYear, curMonth)
    );
    // 上月的开始日期
    let lastMonthStartDate = new Date(curYear, curMonth - 1, 1);
    // 上月的结束日期
    let lastMonthEndDate = new Date(
      curYear,
      curMonth - 1,
      this.getMonthDays(curYear, curMonth - 1)
    );
    // 本季的月份
    let curQuarterMonth = this.getQuarterStartMonth(curMonth);
    // 本季的开始日期
    let curQuarterStartDate = new Date(curYear, curQuarterMonth, 1);
    // 本季的结束日期
    let curQuarterEndDate = new Date(
      curYear,
      curQuarterMonth + 2,
      this.getMonthDays(curYear, curMonth)
    );
    // 上季的月份
    let lastQuarterMonth = curQuarterMonth - 1;
    lastQuarterMonth = lastQuarterMonth < 0 ? 12 : lastQuarterMonth;
    // 上季的开始日期
    let lastQuarterStartDate = new Date(
      lastQuarterMonth > curQuarterMonth ? lastYear : curYear,
      this.getQuarterStartMonth(lastQuarterMonth),
      1
    );
    // 上季的结束日期
    let lastQuarterEndDate = new Date(
      lastQuarterMonth > curQuarterMonth ? lastYear : curYear,
      this.getQuarterStartMonth(lastQuarterMonth) + 2,
      this.getMonthDays(curYear, lastQuarterMonth)
    );
    // 本年的开始日期
    let curYearStartDate = new Date(curYear, 0, 1);
    // 本年的结束日期
    let curYearEndDate = new Date(curYear, 11, 31);
    // 上年的开始日期
    let lastYearStartDate = new Date(lastYear, 0, 1);
    // 上年的结束日期
    let lastYearEndDate = new Date(lastYear, 11, 31);

    let dateOptionsList = [
      {
        label: "今天",
        beginDate: this.formatDate(curDate),
        endDate: this.formatDate(curDate),
      },
      {
        label: "昨天",
        beginDate: this.formatDate(yesterday),
        endDate: this.formatDate(yesterday),
      },
      {
        label: "近7天",
        beginDate: this.formatDate(curDateWeekAgo),
        endDate: this.formatDate(curDate),
      },
      {
        label: "近30天",
        beginDate: this.formatDate(curDateMonthAgo),
        endDate: this.formatDate(curDate),
      },
      {
        label: "本周",
        beginDate: this.formatDate(weekStartDate),
        endDate: this.formatDate(weekEndDate),
      },
      {
        label: "上周",
        beginDate: this.formatDate(lastWeekStartDate),
        endDate: this.formatDate(lastWeekEndDate),
      },
      {
        label: "本月",
        beginDate: this.formatDate(curMonthStartDate),
        endDate: this.formatDate(curMonthEndDate),
      },
      {
        label: "上月",
        beginDate: this.formatDate(lastMonthStartDate),
        endDate: this.formatDate(lastMonthEndDate),
      },
      {
        label: "本季",
        beginDate: this.formatDate(curQuarterStartDate),
        endDate: this.formatDate(curQuarterEndDate),
      },
      {
        label: "上季",
        beginDate: this.formatDate(lastQuarterStartDate),
        endDate: this.formatDate(lastQuarterEndDate),
      },
      {
        label: "本年",
        beginDate: this.formatDate(curYearStartDate),
        endDate: this.formatDate(curYearEndDate),
      },
      {
        label: "上年",
        beginDate: this.formatDate(lastYearStartDate),
        endDate: this.formatDate(lastYearEndDate),
      },
    ];
    this.setData({
      dateOptionsList,
    });
  },
});
